﻿<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	class news_controller extends my_controller
	{
		public function __construct()
        {
            parent::__construct();

            $this->_model->news  = new model_news();
            $this->_model->pages = new model_navbar();
            $this->_helper       = new helper_text();

        }

        public function index()
        {
            $pages_slug = $_GET['slug'];

            if(!isset($pages_slug) || is_numeric($pages_slug) || $pages_slug == '')
            {
                $this->_data['title'] 	  = 'Jewel shop';
                $this->_data['head_name'] = "404";
                $this->_data['content']   = $this->view("errors/errors", $data='', TRUE);
            }
            else
            {
                $data['news'] = $this->_model->news->getAllNewsBySlug(array('pages_slug' => $pages_slug));

                if(!$data['news'])
                {
                    $this->_data['title'] 	  = 'Jewel shop';
                    $this->_data['head_name'] = "404";
                    $this->_data['content']   = $this->view("errors/errors", $data='', TRUE);
                }
                else
                {

                    (array) $page = $this->_model->pages->getPagesBySlug(array('slug' => $_GET['slug']));
                    //NEED CHECK//
                    if(count($data['news']) == 1)
                    {
						$this->_model->news->updateCounter();
					}

                    if(!empty($page)):
                        $head_name = $page['pages_name'];
                    endif;

                    $this->_data['head_name'] = $head_name;

                    $data['pages_name'] 	  = $head_name;

                    $this->_data['title'] 	  = 'Jewel | '. ucfirst($head_name);

                    $this->_data['content']   = $this->view("news/listnews", $data, TRUE);

                }
            }

            $this->view("layout", $this->_data);

        }//End function index())

        public function readnews()
		{
			$slug = $_GET['slug'];

			/*NEED CHECK URL*/

			if(!isset($slug) || is_numeric($slug) || $slug == '')
            {
                $this->_data['title'] 	  = 'Jewel shop';
                $this->_data['head_name'] = "404";
                $this->_data['content']   = $this->view("errors/errors", $data='', TRUE);
            }
            else
            {

            	$slugs = $this->_model->news->getNewsDetail($slug);
				/*echo "<pre>"; print_r($slugs);*/
				if(!$slugs)
                {
                    $this->_data['title'] 	  = 'Jewel shop';
                    $this->_data['head_name'] = "404";
                    $this->_data['content']   = $this->view("errors/errors", $data='', TRUE);
                }
                else
                {
                	$page = $this->_model->pages->getPagesBySlug(array('slug' => $slugs['pages_slug']));

					/*UPDATE COUNTER*/

					$this->_model->news->updateCounter(array('news_id' => $slugs['news_id']));

					/*END UPDATE COUNTER*/

					$data['newslikes'] = $this->_model->news->getNewLike(array('news_title' => $slugs['news_title'], 'news_id' => $slugs['news_id']));

					if(!empty($page)):
                        $head_name = $page['pages_name'];
                    endif;

                    $this->_data['head_name'] = $head_name;

                    $data['pages'] 			  = $page;

					$data['news'] 			  = $slugs;

					//echo "<pre>"; print_r($data);echo "</pre>";

                    $this->_data['title'] 	  = 'Jewel | '. ucfirst($head_name);

                    $this->_data['content']   = $this->view("news/readnews", $data, TRUE);
                }

      		}
			$this->view("layout", $this->_data);

		}//End function read_news();

	}


    /* End of file news_controller.php */
    /* Location: .//D/root/my_framework/controllers/news_controller.php */


